﻿package com.wb.fw;

import java.util.Date;
import java.util.Map;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.NetworkInfo.State;
import android.net.Uri;
import android.os.Environment;
import android.provider.Settings;
import android.text.format.DateFormat;
import android.util.Log;

import com.wiyun.engine.nodes.Director;

public class Util implements IConstants {
	public static boolean DBG_ENABLED = !true;

	public static void DBG_LOG(String str) {
		if (DBG_ENABLED)
			System.out.println(str);
	}

	// ///////////////////////////////////////////////////////
	// Random
	public static int GetRandom(int min, int max) {
		int random = (int) (Math.random() * (max - min) + min);
		// System.out.println("获取随机数["+min+","+max+"] = " + random);
		return random;
	}

	public static int Get0or1() {
		return GetRandom(0, 2);
	}

	// ///////////////////////////////////////////////////////

	// ///////////////////////////////////////////////////////
	// Get String
	public static String GetString(int resId) {
		return GateActivity.sInstance.getResources().getString(resId);
	}

	// ///////////////////////////////////////////////////////

	// ///////////////////////////////////////////////////////
	// Open URL
	public static void OpenURL(final String url) {
		Intent intent = new Intent(Intent.ACTION_VIEW);
		intent.setData(Uri.parse(url));
		Director.getInstance().getContext().startActivity(intent);

	}

	// ///////////////////////////////////////////////////////

	// //////////////////////////////////////////////////////
	// open setting
	public static void OpenSetting() {
		Intent intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS);
		// Intent intent = new Intent("/");
		// ComponentName cm = new
		// ComponentName("com.android.settings","com.android.settings.WirelessSettings");
		// intent.setComponent(cm);
		// intent.setAction("android.intent.action.VIEW");
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		Director.getInstance().getContext().startActivity(intent);
	}

	// //////////////////////////////////////////////////////

	// ///////////////////////////////////////////////////////

	public static final int NETWORK_STATE_DISCONNECTED = -1;
	public static final int NETWORK_STATE_MOBILE3G = 1;
	public static final int NETWORK_STATE_WIFI = 2;

	// get network state
	public static int GetNetWorkState(Context context) {
		int state = NETWORK_STATE_DISCONNECTED;
		ConnectivityManager conm = (ConnectivityManager) context
				.getSystemService(Context.CONNECTIVITY_SERVICE);

		if (conm == null)
			return NETWORK_STATE_DISCONNECTED;
		// mobile 3G Data Network
		State mobile3G = conm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE)
				.getState();
		// wifi
		State wifi = conm.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
				.getState();
		if (mobile3G == NetworkInfo.State.CONNECTED)
			state = NETWORK_STATE_MOBILE3G;
		else if (wifi == NetworkInfo.State.CONNECTED)
			state = NETWORK_STATE_WIFI;

		return state;
	}

	public static boolean IsNetworkAvailable(Context mContext) {
		ConnectivityManager conm = (ConnectivityManager) mContext
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		if (conm != null) {
			NetworkInfo[] info = conm.getAllNetworkInfo();
			if (info != null && info.length > 0) {
				for (int i = 0; i < info.length; i++) {
					if (info[i].getState() == NetworkInfo.State.CONNECTED) {
						return true;
					}
				}
			}
		}
		return false;
	}

	// check sd card
	public static boolean CheckSDCard() {
		return Environment.getExternalStorageState().equals(
				Environment.MEDIA_MOUNTED);
	}

	// get sd card Path
	public static String GetSDCardPath() {
		if (Environment.getExternalStorageState().equals(
				android.os.Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().getAbsolutePath();
		}
		return null;
	}

	public static String[][] getInfiniteRecord(Activity activity,
			int infiniteLevel) {
		SharedPreferences share = activity.getSharedPreferences(infiniteLevel
				+ "-infinite", 0);
		// ////////////
		String[][] top_record = new String[5][2];
		for (int i = 0; i < 5; i++) {
			String[] save = share.getString(Integer.toString(i), "0,0").split(
					",");
			int savetime = Integer.valueOf(save[0]);
			if (savetime == 0) {
				top_record[i][0] = "---";
				top_record[i][1] = "---";
			} else {
				top_record[i][0] = save[0];
				top_record[i][1] = save[1];
			}
		}
		return top_record;
	}

	public static void saveInfiniteRecord(Activity activity, int infiniteLevel,
			int score, int maxLen) {
		SharedPreferences share = activity.getSharedPreferences(infiniteLevel
				+ "-infinite", 0);
		int insertPos = maxLen;
		for (int i = 0; i < maxLen; i++) {
			String[] save = share.getString(Integer.toString(i), "0,0").split(
					",");
			try {
				int savescore = Integer.valueOf(save[0]);
				if (score >= savescore) {
					insertPos = i;
					break;
				}
			} catch (Exception e) {
				// System.out.println("save[0]="+save[0]+",svae[1]"+save[1]);
				e.printStackTrace();
			}
		}
		SharedPreferences.Editor editor = share.edit();
		if (insertPos < maxLen) {
			for (int i = maxLen-1; i > insertPos; i--) {
				String[] save = share.getString(Integer.toString(i), "0,0")
						.split(",");
				editor.putString(Integer.toString(i + 1), save[0] + ","
						+ save[1]);
			}
			String time = (String) DateFormat.format("yyyy-MM-dd",
					System.currentTimeMillis());
			editor.putString(Integer.toString(insertPos), score + "," + time);
		}
		editor.commit();
		// //////////////
		// String[][] top_record = new String[sInfinitePageLen][2];
		// for(int i=0;i<sInfinitePageLen;i++) {
		// String[] save = share.getString(Integer.toString(i),
		// "0,0").split(",");
		// int savetime = Integer.valueOf(save[0]);
		// if (savetime == 0) {
		// top_record[i][0] = "---";
		// top_record[i][1] = "---";
		// } else {
		// top_record[i][0] = save[0];
		// top_record[i][1] = save[1];
		// }
		// }
		// return top_record;
	}
	
	public static int[] getUnlocked(Activity activity, int maxLevel) {
		SharedPreferences share = activity.getSharedPreferences("levellocked", Context.MODE_PRIVATE);
		//////////////
		int[] lockedinfo = new int[maxLevel];
		for(int i=0;i<maxLevel;i++) {
			lockedinfo[i] = share.getInt(Integer.toString(i), LEVEL_LOCKED);
		}
		if (lockedinfo[0] == LEVEL_LOCKED) {
			lockedinfo[0] = LEVEL_UNLOCK;
		}
		return lockedinfo;
	}
	
	public static void saveUnlocked(Activity activity, int[] info) {
		SharedPreferences share = activity.getSharedPreferences("levellocked", 0);
		//////////////
		SharedPreferences.Editor editor = share.edit();
		for(int i=0;i<info.length;i++) {
			editor.putInt(Integer.toString(i), info[i]);
		}
		editor.commit();
	}
	
	
	public static int[][] getUnlocked(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("levellocked", 0);
		//////////////
		int[][] lockedinfo = new int[4][5];
		for(int i=0;i<4;i++) {
			String[] save = share.getString(Integer.toString(i), "0-0-0-0-0").split("-");
			for(int j=0;j<save.length;j++) {
				lockedinfo[i][j] = Integer.valueOf(save[j]);
			}
		}
		if (lockedinfo[0][0] == 0) {
			lockedinfo[0][0] = 1;
		}
		return lockedinfo;
	}
	
	public static void saveUnlocked(Activity activity, int[][] info) {
		SharedPreferences share = activity.getSharedPreferences("levellocked", 0);
		//////////////
		SharedPreferences.Editor editor = share.edit();
		for(int i=0;i<info.length;i++) {
			StringBuffer sb = new StringBuffer();
			for(int j=0;j<info[i].length;j++) {
				sb.append(info[i][j]);
				if (j < info[i].length-1) {
					sb.append("-");
				}
			}
			editor.putString(Integer.toString(i), sb.toString());
		}
		editor.commit();
	}
	///////////////////////////////
	public static void saveGold(Activity activity, int gold) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		SharedPreferences.Editor editor = share.edit();
		editor.putInt("gold", gold);
		editor.commit();
	}
	
	public static int getGold(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		return share.getInt("gold", 0);
	}
	
	public static void saveFirst(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		SharedPreferences.Editor editor = share.edit();
		editor.putBoolean("first", false);
		editor.commit();
	}
	
	public static boolean isFirst(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		return share.getBoolean("first", true);
	}
	
	public static void unlockGougou(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		SharedPreferences.Editor editor = share.edit();
		editor.putBoolean("unlock", true);
		editor.commit();
	}
	
	public static boolean isUnlockGougou(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		return share.getBoolean("unlock", false);
	}
	
	public static void saveAnimType(Activity activity, int type) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		SharedPreferences.Editor editor = share.edit();
		editor.putInt("animtype", type);
		editor.commit();
	}
	
	public static int getAnimType(Activity activity) {
		SharedPreferences share = activity.getSharedPreferences("config", 0);
		return share.getInt("animtype", 0);
	}
	
	/////////////////////////////////
	public static float getScaleX(int basex) {
		return basex*Global.sScaleRate;
	}
	public static float getScaleY(int basey) {
		return basey*Global.sScaleRate;
	}
	
}
